<?php
/**
 * Created by PhpStorm.
 * User: sl
 * Date: 2017/9/14
 * Time: 09:45
 */
namespace App\Mp\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Smart\Controllers\Backend\SysBase;
use Smart\Service\SysFuncService;
use Facades\Smart\Service\ServiceManager;
use Mp\Service\Common\NavManager;

class Mp extends  SysBase{

    protected $navs = [];

    public function __construct(Request $request)
    {   
        parent::__construct($request);

        $this->middleware('auth');

    }

    public function _init($pageTitle = '新页面'){
        parent::_init($pageTitle);
        $SysFuncService = ServiceManager::make( SysFuncService::class );
      //  var_dump($SysFuncService->getMenuByRoles(1,'backend'));
        $this->user = Auth::user();
        $this->_addData(
            'menuData',
        //暂定超级管理员
            $SysFuncService->getMenuByRoles(
                Auth::id(),
                $this->module )
        );
        $this->_addData( 'user', $this->user );

        $this->_makeNav(['title' => '首页','link'=> '/']);

    }

    public function _displayWithLayout( $view = 'index'){
      
        return view($view)->with($this->data)->with('js' , $this->_makeJs())->with('css' , $this->_makeCss());
    }


    protected function _makeNav($navs){
        array_push($this->navs,$navs);
       
        $navManager = new NavManager();
        if($this->navs){
            collect($this->navs)->map(function($val) use ($navManager){
                $navManager->nav(function($nav)  use ($val){
                    collect((array)$val)->map(function($attribute,$key) use ($nav){
                     $nav->$key($attribute);
                    });
                }); 
            });
        }
        $this->_addData('navs',$navManager->render());
        return $this;
    }
}